//
//  TSToastTool.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

import SVProgressHUD

public let TSToastShared = TSToastTool.shared
public let TSToastWindow = TSToastTool.getCurrentWindow()

open class TSToastTool {
    public static let shared = TSToastTool()
    
    private init() {
        SVProgressHUD.swizzleMethods()
//        如果您想要堆叠 HUD，您可以使用以下方法平衡每个节目通话：
//        [SVProgressHUD popActivity];
        SVProgressHUD.setDefaultStyle(.dark)
        SVProgressHUD.setDefaultAnimationType(.native)
        SVProgressHUD.setDefaultMaskType(.clear)
        SVProgressHUD.setMinimumSize(CGSize(width: 100, height: 100))
        SVProgressHUD.setCornerRadius(16.0)
        SVProgressHUD.setBackgroundColor("#000000".uiColor.withAlphaComponent(0.8))
        SVProgressHUD.setForegroundColor(.white)
        SVProgressHUD.setMinimumDismissTimeInterval(3.0)
//        SVProgressHUD.setInfoImage(UIImage())
//        SVProgressHUD.setImageViewSize(CGSize.zero)

    }
    
    
    public func setUIContent(font:UIFont? = nil,infoImage:UIImage? = nil) {
        if let font = font {
            SVProgressHUD.setFont(font)
        }
        
        if let infoImage = infoImage {
            SVProgressHUD.setInfoImage(infoImage)
        }
        
    }

    /// 显示文字提示
    public func showToast(text: String?, duration: TimeInterval = 3.0,containerView:UIView? = nil) {
        if let text = text {
            if text.count == 0 {
                return
            }
            kExecuteOnMainThread {
                if let containerView = containerView {
                    weak var weakContainer = containerView
                    SVProgressHUD.setContainerView(weakContainer)
                }else if let window = Self.getCurrentWindow() {
                    SVProgressHUD.setContainerView(window)
                }
                SVProgressHUD.setMinimumDismissTimeInterval(duration)
                SVProgressHUD.setDefaultMaskType(.none)
                SVProgressHUD.showInfo(withStatus: text)
                
                DispatchQueue.main.asyncAfter(deadline: .now()+duration){
                    SVProgressHUD.setContainerView(nil)
                }
            }
        }
    }
    

    /// 显示加载动画
    public func showLoading(text:String? = nil,containerView:UIView? = WindowHelper.getCurrentWindow()) {
        kExecuteOnMainThread {
            SVProgressHUD.setDefaultMaskType(.clear)
            weak var weakContainer = containerView
            SVProgressHUD.setContainerView(weakContainer)
            SVProgressHUD.show(withStatus: text)
        }
    }

    /// 隐藏加载动画
    public func hideLoading() {
        kExecuteOnMainThread {
            SVProgressHUD.dismiss()
            SVProgressHUD.setContainerView(nil)
        }
    }
    
    /// 显示进度提示
    public func showProgress(progress:Float, status: String?,containerView:UIView?) {
        kExecuteOnMainThread {
            SVProgressHUD.setDefaultMaskType(.clear)
            SVProgressHUD.setContainerView(containerView)
            SVProgressHUD.showProgress(progress, status: status)
        }
    }
    
    /// 获取当前窗口，兼容 iOS 13 及以上版本
    static func getCurrentWindow() -> UIWindow? {
        if #available(iOS 13.0, *) {
            // iOS 13 及以上使用 `scene` 获取当前 window
            guard let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene else {
                return UIApplication.shared.keyWindow
            }
            return scene.windows.first { $0.isKeyWindow }
        } else {
            // iOS 12 及以下直接从 keyWindow 获取
            return UIApplication.shared.keyWindow
        }
    }
}


public func kShowToastDataMissing(){
    TSToastShared.showToast(text: "Data missing")
}
